import 'package:dio/dio.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';
import 'package:get/get.dart';

import '../../../bean/bean_train.dart';
import '../../../net/dio_utils.dart';
import '../model/train_model.dart';

class TrainSynthesizeCheckReportController extends GetxController
    with TrainModel {
  int trainId;

  TrainSynthesizeCheckReportController(this.trainId);

  TrainSynthesizeCheckReportEntity _checkRecoderEntity;
  bool _isLoading = true;
  TrainBean _trainBean;

  TrainBean getTrainBean() {
    return _trainBean;
  }

  bool getIsLoading() {
    return _isLoading;
  }

  TrainSynthesizeCheckReportEntity getTrainUserCheckRecoderEntity() {
    return _checkRecoderEntity;
  }

  CancelToken _signCancelToken;

  Future<bool> requestUserCheckRecoderFunction({bool click = false}) async {
    _isLoading = true;
    if (click) {
      update(["root"]);
    }

    if (_signCancelToken != null && !_signCancelToken.isCancelled) {
      _signCancelToken.cancel();
    }
    _signCancelToken = new CancelToken();
    //答题数据
    ResponseInfo statisRresponseInfo = await DioUtils.instance.postRequest(
        url: HttpHelper.trainSynthesizeReport,
        cancelTag: _signCancelToken,
        jsonMap: {
          "trainId": trainId,
        });
    if (statisRresponseInfo.success) {
      Map data = statisRresponseInfo.data;
      _checkRecoderEntity = TrainSynthesizeCheckReportEntity.fromMap(data);
    }
    _trainBean = await getTrainNetDatils(trainId: trainId);
    _isLoading = false;
    update(["root"]);
    return true;
  }

  void createReportRequest() async {
    _isLoading = true;
    update(["root"]);
    ResponseInfo statisRresponseInfo = await DioUtils.instance.postRequest(
        url: HttpHelper.trainSynthesizeReportAdd,
        cancelTag: _signCancelToken,
        jsonMap: {
          "trainId": trainId,
        });
    if (statisRresponseInfo.success) {
      ToastUtils.showToast("操作成功");
    } else {
      ToastUtils.showToast(statisRresponseInfo.message);
    }
    await requestUserCheckRecoderFunction();
  }
}
